﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Melos;



namespace ConverterWebRole
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    string fileName = Path.GetFileName(FileUploadControl.FileName);


                    // begin
                    CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
                    CloudBlobClient client = account.CreateCloudBlobClient();
                    CloudBlobContainer blobContainer = null;
                    blobContainer = client.GetContainerReference("kepek");
                    blobContainer.CreateIfNotExist();
                    string key = Guid.NewGuid().ToString();
                    string blobUrl = "feltoltottkepek" + "/" + key;
                    CloudBlob blob = blobContainer.GetBlobReference(blobUrl);
                    blob.UploadFromStream(FileUploadControl.PostedFile.InputStream);
                    // end

                    //// TODO : static  constructor
                    //CloudStorageAccount account = CloudStorageAccount.DevelopmentStorageAccount;
                    //ImageDataContext dc = new ImageDataContext(account.TableEndpoint.ToString(), account.Credentials);

                    //// create the table
                    //CloudTableClient cloudTableClient = account.CreateCloudTableClient();
                    //cloudTableClient.CreateTableIfNotExist("images");
                    //ImageEntity image = new ImageEntity { PartitionKey = "images", RowKey = blobUrl, BlobUrl = blobUrl, FileName = fileName };
                    //dc.AddObject("images", image);
                    //dc.SaveChanges();


                    //FileUploadControl.SaveAs(Server.MapPath(@"c:\work\uploaded\") + filename);

                    // TODO : static  constructor
                    ImageDataContext dc = new ImageDataContext(account.TableEndpoint.ToString(), account.Credentials);
                    // create the table
                    CloudTableClient client2 = account.CreateCloudTableClient();
                    client2.CreateTableIfNotExist("images");
                    ImageEntity image = new ImageEntity { PartitionKey = "images", RowKey = key, BlobUrl = blobUrl, FileName = fileName };
                    dc.AddObject("images", image);
                    dc.SaveChanges();

                    WorkQueue workerQueue = new WorkQueue(account);
                    workerQueue.putItem(new WorkQueueItem { maxSize = 256, blobAddress = blobUrl });


                    //CloudQueueClient cloudQueueClient = account.CreateCloudQueueClient();
                    //CloudQueue queue = cloudQueueClient.GetQueueReference("message"); // use small caps
                    //queue.CreateIfNotExist();
                    //queue.AddMessage(new CloudQueueMessage(blobUrl));

                    // StatusLabel.Text = "File uploaded!";

                    ResultQueue resultQueue = new ResultQueue(account);
                    ResultQueueItem resultQueueItem = resultQueue.getItem();
                    ThumbnailImage.ImageUrl = resultQueueItem.blobAddress;
                    //ThumbnailImage.ImageUrl = "file://c:/work/claudia.bmp";

                    resultQueue.itemDone();
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "The file could not be uploaded. Error description: " + ex.Message;
                }
            }
        }


    }
}
